/*
 * NetService.h
 *
 *  Created on: Jan 21, 2013
 *      Author: root
 */

#ifndef NETSERVICE_H_
#define NETSERVICE_H_

#include "net_opration.h"
#include <sys/epoll.h>

#define THREAD_TIME_TICK 3000000 /*thread tick useconds*/
#define DEFAULT_TIMEOUT 10   /*default time out 10 seconds*/
#define DEFAULT_TRANSHEARTBEAT_TIME 10 /*default trans heartbeat time 10 seconds*/
#define LIMIT_MAX_THREADS 64 /*limit max threads*/
#define MAXEVENTS 1024 /*define epool max events*/
#define SENDFILE_SIZE 0x20000  /*default every sendfile size 1 Mbits*/

class NetService
{
public:
	  NetService();
	  virtual
	  ~NetService();

protected:
	  /*
		* init net service args
		*/
	   int init(const char * lisetnip,uint16_t port,int max_connect = DFLT_MIN_CONN_SIZ,int max_threads_count = 32);

	   /*
		* start net service
		*/
	   int start();

	   /*
		* stop net service
		*/
	   int stop();


	  /*
	  *inherit virtual function
	  */
	  virtual          int    log_fmt(int nloglvl,const char * lpszFormat, ...);
	  virtual NEXT_OPRATION   accept_completed(user_token *& token,buf_chunk * sock_buf);
	  virtual NEXT_OPRATION   connect_completed(user_token * token,buf_chunk * sock_buf);
	  virtual NEXT_OPRATION   receive_completed(user_token * token,buf_chunk * sock_buf);
	  virtual NEXT_OPRATION   send_completed(user_token * token,buf_chunk * sock_buf);
	  virtual NEXT_OPRATION   trans_heartbeat(user_token * token);
	  virtual NEXT_OPRATION   trans_completed(user_token * token,buf_chunk * sock_buf);
	  virtual NEXT_OPRATION   recv_or_send_wait_completed(user_token * token,buf_chunk * sock_buf);
	  virtual         void    close_completed(user_token * token,buf_chunk * sock_buf,NET_ERROR_CODE error);

	  /*
	   *net operation function
	   */
	  int post_usr_connect(user_token * token,NETPROTOCL net_protocl);
	  int force_close_connection(u_long connIndex);


	  /*
	  *svr function
	  */
	  int get_connect_pool_count();
	  int get_connect_pool_use_count();
	  int get_connect_pool_unuse_count();
	  int get_buf_pool_count();
	  int get_buf_pool_unuse_count();
	  int get_current_listen_port();
	  const char * get_core_version();

private:
	  /*
	   * init listen socket
	   */
	  int setnonblocking(int sock);
	  void sock_set_cork(int fd);
	  int init_epool();
	  int init_listen();
	  int start_listen();
	  void close_listen();
	  int re_listen();

	  /*
	   * IO operation
	   */
	  int post_tcp_connect(user_token *ptoken);
	  int post_udp_connect(user_token *ptoken);
	  int post_receive(netconnection *pconn);
	  int post_send(netconnection *pconn);
	  int post_transfile(netconnection *pconn);
	  int io_accept();
	  int io_recv(netconnection *pconn);
	  int io_sendfile(netconnection *pconn);
	  int io_send(netconnection *pconn);
	  int close_connect(netconnection *pconn);
	  int io_close(netconnection *pconn);

	  /*
	  *iocp compledted function
	  */
	  int  accept_completed(netconnection *pconn);
	  int  connect_completed(netconnection *pconn);
	  int  receive_completed(netconnection *pconn);
	  int  send_completed(netconnection *pconn);
	  int  trans_completed(netconnection *pconn);
	  int  recv_or_send_wait(netconnection *pconn);
	  int  next_opration(netconnection *pconn,NEXT_OPRATION next_op);

	  /*
	   * net svr thread
	   */
	  static void * io_working_proces(void *pParam);
	  static void * io_control_proces(void *pParam);
	  static void * io_event_proces(void *pParam);
	  static void * io_clean_proces(void *pParam);
	  static void * net_opration_proces(void *pParam);
	  static void * io_sendfile_proces(void *pParam);

private:
	  int svr_run_flag;
	  int listenfd;
	  uint16_t listenport;
	  char listenIP[64];
	  int limit_thread_count; /*limit thread count*/
	  int max_connect_num;

	  /*epoll data*/
	  int epfd;
	  struct epoll_event ev;

	  /*thread data*/
	  pthread_t workThreadId;
	  pthread_t controlThreadId;
	  pthread_t eventThreadId[LIMIT_MAX_THREADS];
	  int n_event_thread_num;
	  pthread_t clearThreadId;
	  pthread_t netoperationThreadId;
	  pthread_t sendThreadId[LIMIT_MAX_THREADS];
	  int n_send_thread_num;

	  /*thread sem*/
	  sem_t io_control_sem;
	  sem_t io_event_sem;
	  sem_t io_send_sem;

	  /*
	  *memory pool
	  */
	  connect_pool  connpool;
	  net_event       evtqueue; /*IO event queue*/
	  net_opration    net_op_queue;   /*net operation queue*/
	  net_event       io_queue; /*IO queue*/
};

#endif /* NETSERVICE_H_ */
